package com.ruoyi.standingbook.cleaning.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.standingbook.cleaning.domain.bo.CleaningBo;
import com.ruoyi.standingbook.cleaning.domain.vo.CleaningPicVo;
import com.ruoyi.standingbook.cleaning.domain.vo.CleaningVo;
import com.ruoyi.standingbook.cleaning.service.ICleaningService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 清扫保洁
 *
 * @author cc
 * @date 2023-04-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/standingbook/cleaning")
public class CleaningController extends BaseController {

    private final ICleaningService iCleaningService;

    /**
     * 查询清扫保洁列表
     */
    @SaCheckPermission("standingbook:cleaning:list")
    @GetMapping("/list")
    public TableDataInfo<CleaningVo> list(CleaningBo bo, PageQuery pageQuery) {
        return iCleaningService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出清扫保洁列表
     */
    @SaCheckPermission("standingbook:cleaning:export")
    @Log(title = "清扫保洁", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CleaningBo bo, HttpServletResponse response) {
        List<CleaningVo> list = iCleaningService.queryList(bo);
        ExcelUtil.exportExcel(list, "清扫保洁", CleaningVo.class, response);
    }

    /**
     * 获取清扫保洁详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("standingbook:cleaning:query")
    @GetMapping("/{id}")
    public R<CleaningVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iCleaningService.queryById(id));
    }

    /**
     * 新增清扫保洁
     */
    @SaCheckPermission("standingbook:cleaning:add")
    @Log(title = "清扫保洁", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CleaningBo bo) {
        return toAjax(iCleaningService.insertByBo(bo));
    }

    /**
     * 修改清扫保洁
     */
    @SaCheckPermission("standingbook:cleaning:edit")
    @Log(title = "清扫保洁", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CleaningBo bo) {
        return toAjax(iCleaningService.updateByBo(bo));
    }

    /**
     * 删除清扫保洁
     *
     * @param ids 主键串
     */
    @SaCheckPermission("standingbook:cleaning:remove")
    @Log(title = "清扫保洁", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iCleaningService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @SaIgnore()
    @GetMapping("/cleaningByDate")
    public R<List<CleaningVo>> cleaningByDate(String date,String community) {
        return R.ok(iCleaningService.queryListByDate(date,community));
    }


    @SaIgnore()
    @GetMapping("/cleaningAfterPic")
    public R<List<CleaningPicVo>> cleaningAfterPic(String date)  {
        return R.ok(iCleaningService.getAfterPicByDate(date));
    }

    @SaIgnore()
    @GetMapping("/cleaningBeforePic")
    public R<List<CleaningPicVo>> cleaningBeforePic(String date)  {
        return R.ok(iCleaningService.getBeforePicByDate(date));
    }
}
